package com.action;

import org.apache.struts.action.ActionForward;

import admin.mode.AdminMode;
import admin.mode.MemberDispatchAction;
import base.Power;
import base.tools.Tools;

public class LoginAction extends MemberDispatchAction {
	public ActionForward userLogin(Power power, AdminMode admin)
			throws Exception {
		String furl = power.getParameter("furl");
		int loginFlag = admin.login();
		if (loginFlag == AdminMode.LOGIN_SUCCESS) {
			if (furl != null && !furl.equals("")) {
				return power.findForward(Tools.toByteName(furl.replaceAll(
						"\\|", "&")), true);
			} else {
				return power.findForward(
						"/FrameAction.do?opt=v_frame_index_main", true);
			}
		} else {
			power.setAttribute("message", getMessage(loginFlag));
			return power.findForward("/login.jsp", false);
		}
	}

	// 退出清除session
	public ActionForward userLogout(Power power, AdminMode admin)
			throws Exception {
		admin.logout();
		return power.findForward("/login.jsp", true);
	}

	public String getMessage(int type) throws Exception {
		String message = "";
		switch (type) {
		case 1:
			message = "用户名错误";
			break;
		case 2:
			message = "密码错误";
			break;
		case 3:
			message = "登录成功";
			break;
		case 7:
			message = "用户未激活";
			break;
		default:
			message = "登录失败";
		}
		return message;
	}
}
